#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

int InitList(SqList* L)
{
	SqList* tmp = (SqList*)malloc(sizeof(ElemType) * MAXSIZE);
	if (tmp == NULL) {
		perror("malloc fail!");
		return -1;
	}
	L->elem = tmp;
	L->lenght = 0;
}

void PrintList(SqList* L)
{
	int i = 0;
	for (i = 0; i < L->lenght; i++) {
		printf("%d\n", L->elem[i]);
	}
}

int ListInsert_Sq(SqList* L, int i, ElemType e)
{
	if (i<1 || i>L->lenght + 1) {
		return 0;
	}
	int tail = L->lenght;
	while (i < tail) {
		L->elem[tail] = L->elem[tail - 1];
		tail--;
	}
	L->elem[i - 1] = e;
	L->lenght++;
	return 1;
}

int ListDelete_Sq(SqList* L, int i, ElemType* e)
{
	if (i<1 || i>L->lenght + 1) {
		return 0;
	}
	int cur = i;
	while (cur < L->lenght) {
		L->elem[cur - 1] = L->elem[cur];
		cur++;
	}
	L->lenght--;
	return 1;
}

int Location_Sq(SqList* L, ElemType e)
{
	int i = 0;
	for (i = 0; i < L->lenght; i++) {
		if (L->elem[i] == e) {
			return i + 1;
		}
	}
}

void ReverseList(SqList* L)
{
	int head = 0;
	int tail = L->lenght - 1;
	while (head < tail) {
		int tmp = L->elem[head];
		L->elem[head] = L->elem[tail];
		L->elem[tail] = tmp;
		head++;
		tail--;
	}
}

int MergeList(SqList La, SqList Lb, SqList* Lc)
{
	Lc->lenght = La.lenght + Lb.lenght;
	int i = 0;
	int j = 0;
	for (i = 0; i < La.lenght; i++) {
		Lc->elem[i] = La.elem[i];
	}
	for (i = La.lenght,j = 0; i < Lc->lenght; i++,j++) {
		Lc->elem[i] = Lb.elem[j];
	}
	return 1;
}